package com.tengju.payment.infrastructure.persistence.mybatis.mapper;


import org.apache.ibatis.annotations.*;
import com.tengju.payment.infrastructure.persistence.mybatis.dataobject.ChannelDO;

import java.time.LocalDateTime;

@Mapper
public interface ChannelMapper {


    String DEFAULT_COLUMN_LIST =
            "id,merchant_no,merchant_name,business_type,status,channel_type,info,extra,create_time,update_time,deleted";


    /**
     * 插入数据
     */
    @Insert("insert INTO pay_channel (" + DEFAULT_COLUMN_LIST +
            ") VALUES(#{id}, #{merchantNo}, #{merchantName}, #{businessType},#{status},#{channelType}, #{info},#{extra},#{createTime},#{updateTime}, 0) ")
    @Options(keyProperty = "id")
    void insert(ChannelDO channelDO);


    /**
     * 修改状态
     */
    @Update(" UPDATE pay_channel SET status = #{afterStatus}" +
            " WHERE id = #{id} AND status = #{beforeStatus} LIMIT 1")
    int updateStatus(@Param("id") long id,
                     @Param("beforeStatus") int beforeStatus,
                     @Param("afterStatus") int afterStatus);
    

    /**
     * 根据主键id获取
     * @param id
     * @return
     */
    @Select(" <script>SELECT " + DEFAULT_COLUMN_LIST + " FROM pay_channel " +
            " WHERE id = #{id}  " +
            " and deleted = 0 limit 1</script>")
    @Results(id = "payChannel",
            value = {
                    @Result(property = "id", column = "id", javaType = Long.class),
                    @Result(property = "merchantNo", column = "merchant_no", javaType = String.class),
                    @Result(property = "merchantName", column = "merchant_name", javaType = String.class),
                    @Result(property = "businessType", column = "business_type", javaType = Integer.class),
                    @Result(property = "status", column = "status", javaType = Integer.class),
                    @Result(property = "channelType", column = "channel_type", javaType = Integer.class),
                    @Result(property = "info", column = "info", javaType = String.class),
                    @Result(property = "extra", column = "extra", javaType = String.class),
                    @Result(property = "createTime", column = "create_time", javaType = LocalDateTime.class),
                    @Result(property = "updateTime", column = "update_time", javaType = LocalDateTime.class),
                    @Result(property = "deleted", column = "deleted", javaType = Boolean.class),
            })
    ChannelDO getById(@Param("id") long id);



    /**
     * 根据业务类型 渠道类型查询
     */
    @Select(" <script>SELECT " + DEFAULT_COLUMN_LIST + " FROM pay_channel " +
            " WHERE business_type = #{businessType} and  channel_type = #{channelType} " +
            " and deleted = 0 limit 1</script>")
    @ResultMap("payChannel")
    ChannelDO getByBusinessChannelType(@Param("businessType") int businessType, @Param("channelType") int channelType);



}
